package com.xcl.controller;

import com.xcl.pojo.Category;
import com.xcl.pojo.Result;
import com.xcl.service.CategoryService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")
//@CrossOrigin//跨域
@Validated
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/findAll")
    public Result findAll(){
        List<Category> list = categoryService.findAll();
        return Result.success(list);
    }
    @RequestMapping("/delete")
    public Result deleteById(Integer id){
        Integer i = categoryService.deleteById(id);
        if (i>0){
            return Result.success();
        }
        return Result.error("删除失败");
    }
    @RequestMapping("/add")
    public Result addCategory(@Valid @RequestBody Category category){
        Integer i = categoryService.addCategory(category);
        if (i>0){
            return Result.success();
        }
        return Result.error("添加失败");
    }

    @RequestMapping("/update")
    public Result update (@Valid @RequestBody Category category){
        Integer i = categoryService.update(category);
        if (i>0){
            return Result.success();
        }
        return Result.error("修改失败");

    }
}
